package com.tristia.resources.impl;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sun.jersey.api.NotFoundException;
import com.tristia.dao.GenericDao;
import com.tristia.entity.Cd;
import com.tristia.entity.Track;

@Component
@Scope("request")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_ATOM_XML })
public class TrackResWs {
	@Resource
	GenericDao<Cd, Long> cdDao;
	@Context 
	UriInfo ui;
	@Context
	Request request;
	private String id;
	private int trackId;
	private Logger log = Logger.getLogger(this.getClass());
	
	public TrackResWs(){
	}
	
	public TrackResWs(UriInfo ui, Request request, String id) {
        this.ui = ui;
        this.request = request;
        this.id = id;
    }
	
	public TrackResWs(GenericDao<Cd, Long> cdDao, UriInfo ui, Request request, String id, int trackId) {
		this.cdDao = cdDao;
        this.ui = ui;
        this.request = request;
        this.id = id;
        this.trackId = trackId;
    }
	
	@GET
	public Track getTrack() {
		log.info("Hit getTrack");
		log.info("ID: "+id);
		if (cdDao == null || id == null) {
			if (cdDao == null) log.info("CdDao is null");
			if (id == null) log.info("id is null");
			throw new NotFoundException("CdDao/id is null");
		} else {
			Track track = cdDao.get(Long.parseLong(id)).getTracks().get(trackId-1);
			if (track == null)throw new NotFoundException("Track not found");
			log.info("Sending: \n\n" + track.toString() + "\n\n");
			return track;
		}
	}
}
